package com.snail.billing.page;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.net.HttpApp;
import com.android.net.HttpResult;
import com.android.net.HttpSession;
import com.android.net.OnHttpResultListener;
import com.android.util.AlertUtil;
import com.android.util.ResUtil;
import com.snail.billing.Billing;
import com.snail.billing.DataCache;
import com.snail.billing.Resource;
import com.snail.billing.data.Account;
import com.snail.billing.data.AccountManager;
import com.snail.billing.json.JsonBase;
import com.snail.billing.os.DialogPage;
import com.snail.billing.session.RegisterCommonSession;
import com.snail.billing.session.RegisterMoblieSendSession;
import com.snail.billing.session.RegisterMoblieSession;
import com.snail.billing.session.RegisterSquenceSession;
import com.snail.billing.util.AccountCaptcha;
import com.snail.billing.util.BillingStringUtil;

public class RegisterPage extends DialogPage implements View.OnClickListener, OnHttpResultListener {

	private TextView textTitle;
	private View buttonBack;

	private View buttonSelectMobile;
	private View buttonSelectUsername;
	private View layoutMobile;
	private View layoutUsername;

	private EditText inputMobileAccount;
	private EditText inputMobileCaptcha;
	private View buttonMobileCaptcha;
	private EditText inputMobilePwd;

	private EditText inputUsernameAccount;
	private EditText inputUsernamePwd;
	private View buttonDelete;
	private View buttonProtocol;
	private View buttonRegister;

	private HttpApp httpApp;
	private RegisterSquenceSession registerSquenceSession;
	private RegisterMoblieSendSession registerMoblieSendSession;
	private RegisterMoblieSession registerMoblieSession;
	private RegisterCommonSession registerCommonSession;

	private int state;
	private String accountString;
	private String pwdString;
	private String userName;

	@Override
	public int createLayoutId() {
		return ResUtil.getLayoutId(Resource.layout.snailbilling_register_activity);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		textTitle = (TextView) findViewById(ResUtil.getViewId(Resource.id.snailbilling_title_text));
		textTitle.setText(ResUtil.getString(Resource.string.snailbilling_register_title));
		buttonBack = findViewById(ResUtil.getViewId(Resource.id.snailbilling_title_button_back));
		buttonBack.setOnClickListener(this);
		//
		buttonSelectMobile = findViewById(ResUtil.getViewId(Resource.id.snailbilling_register_button_select_mobile));
		buttonSelectUsername = findViewById(ResUtil.getViewId(Resource.id.snailbilling_register_button_select_username));
		layoutMobile = findViewById(ResUtil.getViewId(Resource.id.snailbilling_register_layout_mobile));
		layoutUsername = findViewById(ResUtil.getViewId(Resource.id.snailbilling_register_layout_username));
		buttonSelectMobile.setOnClickListener(this);
		buttonSelectUsername.setOnClickListener(this);
		//
		inputMobileAccount = (EditText) findViewById(ResUtil.getViewId(Resource.id.snailbilling_register_input_mobile_account));
		inputMobileCaptcha = (EditText) findViewById(ResUtil.getViewId(Resource.id.snailbilling_register_input_mobile_captcha));
		buttonMobileCaptcha = findViewById(ResUtil.getViewId(Resource.id.snailbilling_register_button_captcha));
		inputMobilePwd = (EditText) findViewById(ResUtil.getViewId(Resource.id.snailbilling_register_input_mobile_pwd));
		buttonMobileCaptcha.setOnClickListener(this);
		//
		inputUsernameAccount = (EditText) findViewById(ResUtil.getViewId(Resource.id.snailbilling_register_input_username_account));
		inputUsernamePwd = (EditText) findViewById(ResUtil.getViewId(Resource.id.snailbilling_register_input_username_pwd));
		buttonDelete = findViewById(ResUtil.getViewId(Resource.id.snailbilling_register_input_username_button_delete));
		buttonDelete.setOnClickListener(this);
		buttonProtocol = findViewById(ResUtil.getViewId(Resource.id.snailbilling_register_button_protocol));
		buttonProtocol.setOnClickListener(this);
		buttonRegister = findViewById(ResUtil.getViewId(Resource.id.snailbilling_register_button_register));
		buttonRegister.setOnClickListener(this);
		//
		httpApp = new HttpApp(getContext());
		httpApp.setOnHttpResultListener(this);
		//
		showState(state);
	}

	@Override
	public void onResume() {
		super.onResume();
		if (userName == null) {
			registerSquenceSession = new RegisterSquenceSession();
			httpApp.request(registerSquenceSession);
		} else {
			inputUsernameAccount.setText(userName);
		}
	}

	private void showState(int state) {
		this.state = state;
		if (state == 0) {
			buttonSelectMobile.setSelected(true);
			layoutMobile.setVisibility(View.VISIBLE);
			buttonSelectUsername.setSelected(false);
			layoutUsername.setVisibility(View.GONE);
		} else if (state == 1) {
			buttonSelectMobile.setSelected(false);
			layoutMobile.setVisibility(View.GONE);
			buttonSelectUsername.setSelected(true);
			layoutUsername.setVisibility(View.VISIBLE);
		}
	}

	@Override
	public void onClick(View v) {
		if (v.equals(buttonBack)) {
			getPageManager().backward();
		} else if (v.equals(buttonSelectMobile)) {
			showState(0);
		} else if (v.equals(buttonSelectUsername)) {
			showState(1);
		} else if (v.equals(buttonMobileCaptcha)) {
			accountString = inputMobileAccount.getText().toString();
			if (!BillingStringUtil.isValidateMobile(accountString)) {
				AlertUtil.show(getContext(), ResUtil.getString(Resource.string.snailbilling_account_validate_mobile));
				return;
			}
			registerMoblieSendSession = new RegisterMoblieSendSession(accountString);
			httpApp.request(registerMoblieSendSession);
		} else if (v.equals(buttonDelete)) {
			inputUsernameAccount.setText("");
		} else if (v.equals(buttonProtocol)) {
			getPageManager().forward(RegisterProtocolPage.class);
		} else if (v.equals(buttonRegister)) {
			if (state == 0) {
				accountString = inputMobileAccount.getText().toString();
				pwdString = inputMobilePwd.getText().toString();
				if (TextUtils.isEmpty(pwdString)) {
					AlertUtil.show(getContext(), ResUtil.getString(Resource.string.snailbilling_register_input_username_pwd));
					return;
				}
				if (!AccountCaptcha.validatePwd(getContext(), accountString, pwdString, pwdString)) {
					return;
				}
				String captcha = inputMobileCaptcha.getText().toString();
				registerMoblieSession = new RegisterMoblieSession(accountString, pwdString, captcha);
				httpApp.request(registerMoblieSession);
			} else if (state == 1) {
				accountString = inputUsernameAccount.getText().toString();
				if (TextUtils.isEmpty(accountString)) {
					AlertUtil.show(getContext(), ResUtil.getString(Resource.string.snailbilling_register_input_username_account));
					return;
				}
				pwdString = inputUsernamePwd.getText().toString();
				if (TextUtils.isEmpty(pwdString)) {
					AlertUtil.show(getContext(), ResUtil.getString(Resource.string.snailbilling_register_input_username_pwd));
					return;
				}
				if (!accountString.equals(userName) && !AccountCaptcha.validateAccount(getContext(), accountString)) {
					return;
				}
				if (!AccountCaptcha.validatePwd(getContext(), accountString, pwdString, pwdString)) {
					return;
				}
				registerCommonSession = new RegisterCommonSession(accountString, pwdString);
				httpApp.request(registerCommonSession);
			}
		}
	}

	@Override
	public void onHttpResult(HttpResult httpResult) {
		if (httpResult.isSucceed()) {
			HttpSession httpSession = httpResult.getHttpSession();
			if (httpSession.equals(registerSquenceSession)) {
				RegisterSquenceSession.JsonData jsonData = new RegisterSquenceSession.JsonData((String) httpSession.getResponseData());
				if (jsonData.getCode() == 1) {
					userName = jsonData.getUserName();
					inputUsernameAccount.setText(userName);
				}
			} else if (httpSession.equals(registerMoblieSendSession)) {
				String reponse = (String) httpSession.getResponseData();
				if (reponse.equals("1")) {
					inputMobileAccount.setEnabled(false);
					Toast.makeText(getContext(), "发送成功", Toast.LENGTH_SHORT).show();
				} else if (reponse.equals("2")){
					Toast.makeText(getContext(), "服务器忙，请稍后再试", Toast.LENGTH_SHORT).show();
				} else if (reponse.equals("3")){
					Toast.makeText(getContext(), "该手机已注册", Toast.LENGTH_SHORT).show();
				}
				// return 1---发送成功;2---发送间隔120s;3---手机已注册;0---参数不能为空
			} else if (httpSession.equals(registerMoblieSession)) {
				String reponse = (String) httpSession.getResponseData();
				JsonBase jsonBase = new JsonBase(reponse);
				if (jsonBase.getCode() == 1) {
					Account currentAccount = new Account();
					currentAccount.setAccount(accountString);
					currentAccount.setPwd(pwdString);
					currentAccount.setType(Account.TYPE_MOBILE);
					AccountManager.setAccount(currentAccount);
					getPageManager().finish();
					DataCache.getInstance().importParams.pageClass = DialogChangePage.class;
					Intent intent = new Intent(getContext(), Billing.class);
					intent.putExtra(DialogChangePage.ACCOUNT, accountString);
					intent.putExtra(DialogChangePage.PWD, pwdString);
					intent.putExtra(DialogChangePage.TYPE, Account.TYPE_MOBILE);
					getContext().startActivity(intent);
				} else {
					Toast.makeText(getContext(), jsonBase.getMessage(), Toast.LENGTH_SHORT).show();
				}
			} else if (httpSession.equals(registerCommonSession)) {
				String reponse = (String) httpSession.getResponseData();
				RegisterCommonSession.XmlData xmlData = new RegisterCommonSession.XmlData(reponse);
				if ("1".equals(xmlData.getCode())) {
					Account currentAccount = new Account();
					currentAccount.setAccount(accountString);
					currentAccount.setPwd(pwdString);
					AccountManager.setAccount(currentAccount);
					getPageManager().finish();
					DataCache.getInstance().importParams.pageClass = DialogChangePage.class;
					Intent intent = new Intent(getContext(), Billing.class);
					intent.putExtra(DialogChangePage.ACCOUNT, accountString);
					intent.putExtra(DialogChangePage.PWD, pwdString);
					intent.putExtra(DialogChangePage.TYPE, Account.TYPE_COMMON);
					getContext().startActivity(intent);
				} else {
					Toast.makeText(getContext(), xmlData.getMessage(), Toast.LENGTH_SHORT).show();
				}
			}
		}
	}

}
